using System.Collections.Generic;
using System.IO;
using System.Web.Routing;
using Nop.Core;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Media;
using System.Linq;

namespace Nop.Plugin.Widgets.ForumLast
{
    /// <summary>
    /// PLugin
    /// </summary>
    public class ForumLastPlugin : BasePlugin, IWidgetPlugin
    {
        private readonly IPictureService _pictureService;
        private readonly ISettingService _settingService;
        private readonly IWebHelper _webHelper;
        private readonly ForumLastSettings _forumLastSettings;

        public ForumLastPlugin(IPictureService pictureService,
            ISettingService settingService, IWebHelper webHelper, ForumLastSettings forumLastSettings)
        {
            this._pictureService = pictureService;
            this._settingService = settingService;
            this._webHelper = webHelper;
            this._forumLastSettings = forumLastSettings;
        }

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public IList<string> GetWidgetZones()
        {
            //return new List<string>() { "left_side_column_before" };
            var widgetZonesList = new List<string>() { _forumLastSettings.ZonesOptions.ToString() }.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
            return widgetZonesList;

        }

        /// <summary>
        /// Gets a route for provider configuration
        /// </summary>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "WidgetsForumLast";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Widgets.ForumLast.Controllers" }, { "area", null } };
        }

        /// <summary>
        /// Gets a route for displaying widget
        /// </summary>
        /// <param name="widgetZone">Widget zone where it's displayed</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PublicInfo";
            controllerName = "WidgetsForumLast";
            routeValues = new RouteValueDictionary()
            {
                {"Namespaces", "Nop.Plugin.Widgets.ForumLast.Controllers"},
                {"area", null},
                {"widgetZone", widgetZone}
            };
        }

        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {

            //settings
            var settings = new ForumLastSettings()
            {
                ZonesOptions = BlockZones.left_side_column_before,
                Publish = true,
                DisplayOrder = 40,
                DirectionOptions = ForumLastDirection.up,
                Interval = 2500,
                Visible = 3,
                ShowNavigation = true,
            };
            _settingService.SaveSetting(settings);

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Title", "News Ticker");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Message", "message");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.WidgetsZone", "Widget zone");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.WidgetsZone.hint", "Select widgets zone");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Publish", "Active");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Publish.hint", "Active");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.DisplayOrder", "Display Order");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.DisplayOrder.hint", "Display Order");
           
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Direction", "Direction"); 
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Direction.hint", "Direction");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Visible", "Visible number of news items");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Visible.hint", "Visible number of news items");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Interval", "Interval (ms)");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.Interval.hint", "Interval (ms)");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.ShowNavigation", "Show navigation buttons");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ForumLast.ShowNavigation.hint", "Show navigation buttons");

            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //settings
            _settingService.DeleteSetting<ForumLastSettings>();

            //locales
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Title");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Message");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Publish");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Publish.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.DisplayOrder");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.DisplayOrder.hint");
            
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Direction");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Direction.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Visible");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Visible.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Interval");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.Interval.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.ShowNavigation");
            this.DeletePluginLocaleResource("Plugins.Widgets.ForumLast.ShowNavigation.hint");

            base.Uninstall();
        }
    }
}
